//
//  BKMain_CycleScrollViewCell.m
//  bestkeep
//
//  Created by utouu666 on 17/2/14.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKMain_CycleScrollViewCell.h"
#import "BKMainCommon.h"

@interface BKMain_CycleScrollViewCell()<SDCycleScrollViewDelegate>

@property(nonatomic,strong)NSMutableArray *imageArray;
@property(nonatomic,strong)NSMutableArray *idArray;
@property(nonatomic,strong)UIScrollView *myScrollView;
@property(nonatomic,strong)SDCycleScrollView *cycleScrollView;
@property(nonatomic,strong)NSString *type;

@end

@implementation BKMain_CycleScrollViewCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

-(void)initialize{
    _myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), self.frame.size.height)];
    _myScrollView.showsVerticalScrollIndicator = NO;
    _myScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_myScrollView];
    
    
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), self.frame.size.height) delegate:self placeholderImage:[UIImage imageNamed:@"BKMain_topbanner"]];
    self.cycleScrollView.currentPageDotColor = RGBACOLOR(27, 188, 155, 1);
    self.cycleScrollView.pageDotColor = [UIColor lightGrayColor];
    self.cycleScrollView.imageURLStringsGroup = _imageArray;
    [self.myScrollView addSubview:self.cycleScrollView];
    
}



- (void)fillData:(id)data{
    if (!data) {
        return;
    }
    
    _imageArray = [NSMutableArray array];
    _idArray = [NSMutableArray array];
    NSArray *dataArr = data[@"data"];
    self.type = data[@"type"];
    for (NSDictionary *dic in dataArr) {
        [_imageArray addObject: [dic objectForKey:@"img"]];
        [_idArray addObject:dic];
    }
    self.cycleScrollView.imageURLStringsGroup = _imageArray;
    self.myScrollView.frame = CGRectMake(0, 0, ScreenWidth, self.frame.size.height);
    self.cycleScrollView.frame = CGRectMake(0, 0, ScreenWidth, self.frame.size.height);
    
}


+(CGSize)calculateSize:(id)data{
    if(!data || ![data isKindOfClass:[NSArray class]]) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(SCREENWIDTH, 145*SCREENWIDTH/375.0f);

}
#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
    [self pushToActive:index];
}
#pragma mark - 首页活动轮播事件
-(void)pushToActive:(NSInteger)index{
    
    BKMainViewController *mainPage  = (BKMainViewController *)self.nextResponder.nextResponder.nextResponder;
    NSDictionary *dic = _idArray[index];
    BannerModel *model = [[BannerModel alloc] init];
    [model setValuesForKeysWithDictionary:dic];
    
    BKMain_WebViewController *h5 = [[BKMain_WebViewController alloc]init];
    h5.webViewUrl = model.code;
    h5.webViewTitle = model.name;
    h5.type = model.type;
    h5.shareFlag = [NSString stringWithFormat:@"%@",model.shareFlag];
    h5.shareCode = model.shareCode;
    h5.shareImg = [NSString stringWithFormat:@"%@",model.shareImg];;
    h5.shareTitle = [NSString stringWithFormat:@"%@",model.shareTitle];;
    h5.shareContent = [NSString stringWithFormat:@"%@",model.shareContent];;
    h5.type_Share = self.type;
    if ([[NSString stringWithFormat:@"%@",model.type] isEqualToString:@"4"]) {
        [mainPage.navigationController pushViewController:h5 animated:YES];
    }else if ([[NSString stringWithFormat:@"%@",model.type] isEqualToString:@"6"]){
        if ([BKMainCommon islogin]) {
            [mainPage.navigationController pushViewController:h5 animated:YES];
        }else{
            Mediator *mediator = [Mediator sharedInstance];
            [mediator userGoToLoginFromOrignalController:mainPage finishBlock:^ {
                
            }];
            
        }
    }else{
        [mainPage pushWithType:model.type code:model.code name:model.name saleTypes:@""];
    }
    
}

@end
